/**
*  \file menu.c
*
*  \brief
*
*  \author Dajin Li
*
*
*/
/*===============================================[ private includes  ]================================================*/

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "menu.h"
#include "lcd.h"
#include "key.h"



/*===============================================[ private defines   ]================================================*/
#define PWD_STATE_INIT                          1
#define PWD_STATE_ENTER                         2
#define PWD_STATE_VERIFY                        3
#define PWD_LEN                                 4
#define ADMIN_PWD_LEN                           6
#define PIN_PWD_LEN                             6
#define PWD_MAX_VALUE                           0x0F
#define PIN_MAX_VALUE                           0x04
#define LindePsw		                        0x121934
#define TechPwdFlag			                    BIT0			/* 0: wrong		            1:password ok	*/
#define ServicePwdFlag				            BIT1			/* 0: wrong		            1:comfirm ok	*/
#define AdminPwdFlag				            BIT2	        /* 0: wrong                 1:ok	        */
// step value                                   
#define STEP_1                                  5
#define STEP_2                                  10
#define STEP_3                                  15
#define STEP_4                                  20
// menu type                                    
#define MAX_MENU_LEVELS	                        10
#define MAX_MENU_ITEM_PER_PAGE                  4
#define MAX_MENU_ITEM_PER_PAGE1                 2

#define MENU_ITEM_TYPE0                         0              //default menu
#define MENU_ITEM_TYPE1                         1              //editable menu is current selected 
#define MENU_ITEM_TYPE2                         2              //editable menu is current editing
#define MENU_ITEM_TYPE3                         3              //choose menu type in choose state
#define MENU_ITEM_TYPE4                         4              //choose menu type in not choose state
#define MENU_ITEM_TYPE5                         5              //choose menu type, responding with enter button
#define MENU_ITEM_TYPE6                         6              //check box
#define MENU_ITEM_TYPE7                         7              //error item

#define KEY_MASK                                0x1F           // enter key, back key, up key, down key, both up&down key pressed
#define FLASH_TIME_1                            5
#define FLASH_TIME_2                            14
#define VAL_FLASH_TIME_1                        3
#define VAL_FLASH_TIME_2                        6

#define MENU_ITEM_TYPE_MASK                     0x000F         // item type
#define MENU_ITEM_BACKGROUND_MASK               0x0070         // background type
#define MENU_ITEM_ABR_MASK                      0x0080         // abbrivation
#define MENU_ITEM_ERROR_MASK                    0x0300         // error mask
#define MENU_ITEM_ACCESS_MASK                   0x0C00         // access mask



#define MENU_ITEM_ACCESS1                       0x0400         // access 1, only access by service

#define PWD_CLEAR_WIDTH                         15 
#define KM_TO_MPH_FLOAT                         0.6214
#define MPH_TO_KM_FLOAT                         1.6093
#define MENU_ZONE_WIDTH2                        260
#define TECK_MENU_ARRAY_INDEX                   0
#define ERROR_MENU_ARRAY_INDEX                  1
#define ERROR_SER_MENU_ARRAY_INDEX              0
#define ERROR_MARK_OFFSET                       135
#define PIN_CODE_NUM                            16

#define DAY_MONTH_YEAR                          0
#define MONTH_DAY_YEAR                          1
#define DIRVER_DIR_MASK                         0x60                  

#ifdef S1D19600
// position
#define PWD_BLANK_OFFSET                        6
#define ERROR_ROW_OFFSET                        33
#define OSHA_ROW_OFFSET                         1
#define PWD_NUM_WIDTH                           10
#define SPEED_METER_VALUE_COL                   120
#define MENU_ZONE_WIDTH                         260
#define MENU_ZONE_HEIGHT                        49
#define MENU_ZONE_HEIGHT1                       99
#define TIME_ROW_OFFSET                         21
#define TIME_ROW_OFFSET_1                       6
#define DATE_ROW_OFFSET                         21
#define DATE_ROW_OFFSET_1                       11
#define DATE_ROW_OFFSET_2                       6
#define UNITS_COL_OFFSET                        19
#define UNITS_COL_OFFSET1                       18
#define DATE_TIME_ROW_OFFSET                    44
#define DATE_YEAR_ROW_OFFSET                    52
#define COL_OFFSET                              71
#define PIN_CODE_COL_OFFSET                     1
#define PIN_CODE_ROW_OFFSET                     26
#define PIN_PWD_ROW_OFFSET                      36
#define LINE_OFFSET_LINE                        28
#define LINE_50_OFFSET                          40
#define PWD_LINE_START                          1
#define PASSWORD_ROW_OFFSET                     55          // (130- 60)/2+30-10
#define CHANGE_PWD_ROW_OFFSET                   75         //  (130- 40)/2+30
#define TILT_ANGLE_SENSOR_ACC                   198
#define OSHA_COL                                40
#define INFO_COL_OFFSET1                        5
#endif


#ifdef ST7511
// position
#define PWD_BLANK_OFFSET                        6
#define ERROR_ROW_OFFSET                        33
#define OSHA_ROW_OFFSET                         1
#define PWD_NUM_WIDTH                           10
#define SPEED_METER_VALUE_COL                   280
#define STEER_ANGLE_VALUE_COL                   364
#define MENU_ZONE_WIDTH                         260
#define MENU_ZONE_HEIGHT                        49
#define MENU_ZONE_HEIGHT1                       99
#define TIME_ROW_OFFSET                         21
#define TIME_ROW_OFFSET_1                       6
#define DATE_ROW_OFFSET                         21
#define DATE_ROW_OFFSET_1                       11
#define DATE_ROW_OFFSET_2                       6
#define UNITS_COL_OFFSET                        19
#define UNITS_COL_OFFSET1                       18
#define INFO_COL_OFFSET1                        5
#define DATE_TIME_ROW_OFFSET                    44
#define DATE_YEAR_ROW_OFFSET                    52
#define COL_OFFSET                              231
#define PIN_CODE_COL_OFFSET                     1
#define PIN_CODE_ROW_OFFSET                     26
#define PIN_PWD_ROW_OFFSET                      36                   // (160 - 44) / 2 - 22
#define LINE_OFFSET_LINE                        28
#define LINE_50_OFFSET                          40
#define PWD_LINE_START                          1
#define PASSWORD_ROW_OFFSET                     55          // (130- 60)/2+30-10
#define CHANGE_PWD_ROW_OFFSET                   75         //  (130- 40)/2+30
#define TILT_ANGLE_SENSOR_ACC                   358
#define OSHA_COL                                200
#define WARNING_DIALOG_ROW                      38
#define WARNING_DIALOG_COL                      241
#define WARNING_DIALOG_H                        116
#define WARNING_DIALOG_W                        222

#endif

#define DISPLAY_CODE                            0
#define TRACR_CODE                              1
#define TRACL_CODE                              2


/*===============================================[ private datatypes ]================================================*/

/*===============================================[ private variables ]================================================*/

/*===============================================[ private functions ]================================================*/

//static void InputNewPassword(void);
//static void ConfirmPassword(void);
//static void LCD_Adj(void);
//static void menu_run(void);
//static void Display_Keyon_Hours(void);
//static void Display_Seat_Hours(void);
//static void Display_Eng_Hours(void);
//static void Language_Select (void);
//static void Units_Select (void);
//static void DisplayMenu(void);
//static void Display_Sysinfo(void);
//static void Select_Date_Format(void);
//static void Set_Time(void);
//static void Set_Date(void);
//static u16  get_date_item(void);
//static void save_date_item(u16 data);
//static void get_date_value_range(u16 * min, u16 * max);
//static u16  get_time_item(void);
//static void save_time_item(u16 data);
//static u16 get_max_value(void);
//static void DispDisplayActiveError(void);
//static void DispDisplayActiveErrorDetail(void);
//static void DispDisplayStoredErrorDetail(void);
//static void DispDisplayStoredError(void);
//static void Display_Pwd(u8 *str,s16 row,s16 column, u8 highlight);
//static void Display_Pin(u8 *str,s16 row,s16 column, u8 highlight);
//static void Display_Pwd_1(u8 *str,s16 row,s16 column, u8 highlight);
//static void Display_Pwd_2(u8 *str,s16 row,s16 column, u8 highlight);
//static void UpdateValue(void);
//static void displayBar(void);
//static void StatusSelection(void);
////static void Display_Weight_Angle(s16 row, s16 column, u8 invert);
////static void Display_Weight_Angle_1(s16 row, s16 column, u8 invert);
//static void DisplayRestore(void);
//static void Display_Bar_Speed(void);
//static void DispGray(void);
//static void display_Tab(void);
//static void display_Submenudivider(void);
//static void Display_Pressure_Temp(s16 row, s16 column, u8 invert);
//static void Display_Pressure_Temp_1(s16 row, s16 column, u8 invert);

//static void UpdateFaultMenu(void);
//static u8 hasActiveError(void);
//static void DispDisplayStoredErrorRestore(void);
//static void Display_Maintenance_Home(s16 row, s16 column, u8 invert);
//static void Display_Maintenance_Home_1(s16 row, s16 column, u8 invert);

/*================================================[ inline functions ]================================================*/

/*================================================[ public functions ]================================================*/

/*================================================[ public variables ]================================================*/
u8 seatBelt;
u8 seatBeltData;
u8 enClearMenu;
u8 MenuItemIndex = 0;
u8 nMenuLevel = 0;
u8 data_mode = 0;
u8 LineIndex = 0;
s16 temp_key_vector = 0;
u8 date_hours = 15;                   // hours
u8 date_minutes = 20;                 // minutes
u8 date_seconds = 20;                 // seconds
u8 date_am = 0;                       // to display am or pm
u8 date_am_data = 0;                       // to display am or pm
u8 hour_format_12H = 0;               // 12H/24H
u8 hour_format_12H_data = 0;          // 12H/24H
u8 date_format = 0;   // 12/31/2015, or 2015/12/31 or 31/12/2015
u8 date_day = 21;
u8 date_month = 12;
u8 date_year = 15;
u8 date_week = 0;
u8 date_format_data = 0;   // 12/31/2015, or 2015/12/31 or 31/12/2015
u8 set_time_flag = 0;
u8 regulate_time_flag = 0;
s16 max_traction_speed_forw_value_request;
s16 max_traction_speed_forw_kwp = 0;

s16 max_traction_speed_forw_value = 0;
s16 max_traction_speed_forw_data = 0;

s16 max_traction_speed_back_value_request = 0;
s16 max_traction_speed_back_value = 0;
s16 max_traction_speed_back_kwp = 0;

s16 max_traction_speed_back_data = 0;
s16 safety_speed_value = 1;
s16 safety_speed_data = 1;

s16 reverse_speed_value = 1;
s16 reverse_speed_data = 1;


s16 area_back_speed_dly_value = 1;
s16 area_back_speed_dly_data = 1;


s16 accel_decel;  // for area speed control
s16 accel_decel_data;  // for area speed control

s16 points1_volts_data;  // volts
s16 points2_volts_data;  // volts
s16 points3_volts_data;  // volts

s16 points1_volts_value;  // volts
s16 points2_volts_value;  // volts
s16 points3_volts_value;  // volts


s16 points1_distance_data;  // distance
s16 points2_distance_data;  // distance
s16 points3_distance_data;  // distance

s16 points1_distance_value;  // distance
s16 points2_distance_value; // distance
s16 points3_distance_value;  // distance


s16 rfid_timeout;
s16 rfid_timeout_data;


s16 max_traction_speed_kp = 1;
s16 max_traction_speed_kp_data = 1;
s16 max_traction_speed_ki = 1;
s16 max_traction_speed_ki_data = 1;
s16 max_traction_speed_kd = 1;
s16 max_traction_speed_kd_data = 1;
s16 max_engine_speed_value = 1;
s16 act_max_engine_speed_value = 1;
s16 min_engine_speed_value = 1;
u16 speedometer_units_data;      // 0: km 1: miles
u8 LanguageSelect_data;
s8 status_select1 = 0;
s8 status_select2 = 0;
s8 status_select1_data = 0;
s8 status_select2_data = 0;
u8 cal_display = 1;
s16 cal_timeout = 0;
u8 lcd_bklight_data = 0;
u16 lcd_bklight_pwm = 0;
u8 cal_sns_page = 0;
s8 current_select_area;
s16 service_hours1 = 0;
s16 service_hours2 = 0;
s16 service_hours3 = 0;
s16 service_hours4 = 0;
s16 service_days1 = 0;
s16 service_days2 = 0;
s16 service_days3 = 0;
s16 service_days4 = 0;
u16 pin_code[PIN_CODE_NUM];
u16 pin_enable;
u8 finalCalStep = 0;
u8 error_display = 0;
u16 CANopen_clearScreen;
u8 J1939_clearScreen;
u8 clearScreen;
u8 pre_cal_current_select_area = 5;
s8 menu_needs_ee;
s16 menu_ee_index;
u16 br1219_engine_total_stored_fault;
u16 br1285_pump_total_stored_fault;
u16 br1285_traction_total_stored_fault;
u16 br1275_bms_total_stored_fault = 0;
u16 rfid_total_stored_fault = 0;
u16 pin_enable;
s8 current_select_bar = 0;
u8 display_active_fault_index;
u16 display_active_fault_code[MAX_DISPLAY_FAULT];
E_STORED_FAULT_PARAM display_stored_fault[MAX_DISPLAY_FAULT];
u8 restore_state = 0;
u8 fault_msg_disp_buffer[200];
u16 br1347_trac_total_stored_fault = 0;
u16 br1347_lift_total_stored_fault = 0;
u32 pre_can_obj_id = 0;
u32 cur_can_obj_id = 0;
u8 dispError;
enum var_tbl_disp_options display_options;
enum var_tbl_disp_options display_options_data;
u8 fuel_sensor_type;
u8 fuel_sensor_type_data;
u16 creep_speed;
u16 creep_speed_data;
u32 fault_ack = 0;
bool LCDShowOnce = FALSE;
s32 eng_hours;
u8 show_distance_enable = 1;
u8 reverse_distance_disp_flag = 0;

/***********************************************************************************************************************
*                             Copyright 2016 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/



